package cn.ikun.web;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter (urlPatterns = "/ikun/*")
public class CoreFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        //执行拦截的逻辑代码
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String uri = request.getRequestURI();
        if("/ikun/login.jsp".equals(uri) || uri.endsWith(".ico") ||uri.endsWith(".png")||uri.endsWith(".css")||uri.endsWith(".jfif")||uri.endsWith(".jpg")){
            chain.doFilter(req,resp);
            return;
        }

        HttpSession session = request.getSession();
        Object uid = session.getAttribute("UID");
        if(uid ==null){
            //说明没有登录
            response.sendRedirect("/ikun/login.jsp");
            return;
        }
        chain.doFilter(req,resp);
    }

    @Override
    public void destroy() {

    }
}
